package servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modelo.bean.Trajeto;

import testeMapas.FachadaDirectionsResult;

public class CadastraCaronaServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String param = request.getParameter("param");
		String start = request.getParameter("start");
		String end = request.getParameter("end");

	//	String num = request.getParameter("num");

		//if (num == null) {			
			System.out.println("Cadastra carona:");
			response.setContentType("text/xml");
			String[] temp = param.split(",");
			for (int i = 0; i < temp.length; i++) {
				//System.out.println("i[" + i + "]: " + temp[i]);
			}
			System.out.println("taaamanaho: " + temp.length);
			Trajeto result = null;
			
			System.out.println("entrada: " + param);
			
			result = new Trajeto(param);
			result.setStart(start);
			result.setEnd(end);
			//result.routes = request.getParameter("routes");
			FachadaDirectionsResult fachada = FachadaDirectionsResult
					.getInstance();
			fachada.inserir(result);
			System.out.println("codigo: " + result.getId_trajeto());
			System.out.println(fachada.procurarPorCodigo(result.getId_trajeto()).getEnd());
			System.out.println(fachada.procurarPorCodigo(result.getId_trajeto()).getStart());

/*
			} else {
			System.out.println("Lista carona: " +  num);
			FachadaDirectionsResult fachada = FachadaDirectionsResult
					.getInstance();
			String result = null;
			DirectionsResult d = fachada.procurarPorCodigo(Integer
					.parseInt(num));
			for (LatLng lg : d.getPontos()) {
				if (result == null) {
					 result = lg.getLat() + "," + lg.getLng() + ",";
				} else {
					 result += lg.getLat() + "," + lg.getLng() + ",";
				}
			}		
			response.setContentType("text/plain");
			response.getWriter().write(result);
		}
			//*/
	}

}